package com.xiaogao.serviceEdu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaogao.commonUtils.Result;
import com.xiaogao.serviceEdu.entity.EduTeacher;
import com.xiaogao.serviceEdu.entity.vo.TeacherQuery;
import com.xiaogao.serviceEdu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 小高
 * @since 2021-05-29
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/serviceEdu/teacher")
//@CrossOrigin // 允许跨域访问
public class EduTeacherController {


    @Autowired
    private EduTeacherService teacherService;


    @ApiOperation("查询所有讲师列表")
    @GetMapping("/findAll")
    public Result findAllTeacher() {
        // 调用service的方法实现查询所有的操作
        List<EduTeacher> eduTeachers = teacherService.list(null);

        return Result.ok().data("items", eduTeachers);
    }

    @ApiOperation("根据ID删除讲师")
    @DeleteMapping("/{id}")
    public Result removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error().message("删除失败！");
        }
    }

    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("/pageTeacher/{page}/{limit}")
    public Result pageTeacher(
            @ApiParam(name = "page", value = "当前页", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页显示条数", required = true)
            @PathVariable Long limit) {

        Page<EduTeacher> pageParam = new Page<>(page, limit);
        // 调用方法时底层自动封装，把分页所有的数据封装到pageParam对象中
        teacherService.page(pageParam, null);

        List<EduTeacher> records = pageParam.getRecords(); // 当前页数据list集合
        long total = pageParam.getTotal();  // 总记录数（所有页的）

        return Result.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("多条件组合分页查询讲师列表")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public Result pageTeacherCondition(
            @ApiParam(name = "page", value = "当前页", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页显示条数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false) TeacherQuery teacherQuery) { // @RequestBody必须是PostMapper

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        teacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return Result.ok().data("total", total).data("rows", records);

    }

    @ApiOperation("添加讲师对象")
    @PostMapping("/addTeacher")
    public Result addTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师对象",required = true)
            @RequestBody EduTeacher eduTeacher){

        boolean save = teacherService.save(eduTeacher);

        return save?Result.ok():Result.error();
    }

    @ApiOperation("根据ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return Result.ok().data("item",eduTeacher);
    }

    @ApiOperation("根据ID修改讲师")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(
            @ApiParam(name = "eduTeacher",value = "讲师对象",required = true)
            @RequestBody EduTeacher eduTeacher){

        boolean update = teacherService.updateById(eduTeacher);
        return update?Result.ok():Result.error();
    }

}

